/*
 * name: paging-load.js
 * version: v0.0.1
 * update: build
 * date: 2017-03-27
 */
define('paging-load', function(require, exports, module) {
	"use strict";
	var $ = require('jquery'),
		def = {
			url: null,
			size: 6,
			data: {},
			success: null,
			nomore: null,
			error: null
		},
		pagingLoad = function(option) {
			var opt = $.extend({}, def, option || {}),
				sendParam = $.extend(true, {}, opt.data),
				trueUrl,
				thisProcessIndex;
			if (!opt.url) {
				return console.warn('toload()参数缺少url');
			}
			trueUrl = opt.url + '?' + $.param(opt.data);
			
			var init = function() {
				var processes = pagingLoad.prototype.process,
					i = 0,
					n = processes.length,
					getPage;
				for (; i < n; ++i) {
					if (processes[i].url == trueUrl) {
						thisProcessIndex = i;
						getPage = processes[i].getPage;
						break;
					}
				}
				if (!getPage) {
					getPage = pagingLoad.prototype.newGetPage();
					processes.push({
						url: trueUrl,
						getPage: getPage
					});
					pagingLoad.prototype.process = processes;
				}
				return getPage;
			};

			return {
				load: function() {
					sendParam.page_index = init()();
					sendParam.page_size = opt.size;
					$.ajax({
						type: 'get',
						url: opt.url,
						data: sendParam,
						dataType: opt.dataType || 'json',
						success: function(res) {
							if ($.isPlainObject(res) && res.status === 'Y' || (res && opt.dataType != 'json')) {
								typeof(opt.success) === 'function' && opt.success(res);
								if ($.isPlainObject(res) && res.data && res.count) {
									var listLength = res.data.split ? JSON.parse(res.data).length : res.data.length;
									if (listLength + sendParam.page_size * (sendParam.page_index - 1) >= parseInt(res.count)) {
										typeof(opt.nomore) === 'function' && opt.nomore();
									}
								}
							} else {
								console.log('数据异常页码回退');
								getPage(true);
								typeof(opt.success) === 'function' && opt.success(res);
							}
						}
					});
				},
				reload: function() {
					if (thisProcessIndex !== void 0) {
						var processes = pagingLoad.prototype.process;
						processes.splice(thisProcessIndex, 1);
						pagingLoad.prototype.process = processes;
					}
				},
				destroy: function() {
					if (thisProcessIndex !== void 0) {
						this.reload();
						delete this.init;
						delete this.load;
						delete this.reload;
						delete this.destroy;
					}
				}
			};
		};

	pagingLoad.prototype.newGetPage = function() {
		var loadPage = 0,
			func = function(pullback) {
				if (pullback) {
					return --loadPage;
				}
				return ++loadPage;
			};
		return func;
	};
	pagingLoad.prototype.process = [];

	module.exports = pagingLoad;
});